﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Library;
using System.Data.SqlClient;

namespace QLKH.PopUp.Khachhang
{
    public partial class popLapdongho : Form
    {
        private Int64 idcus = -1;
        public popLapdongho(Int64 id)
        {
            InitializeComponent();
            idcus = id;
        }

        private void binCombo(ComboBox cb, DataTable dt, string name, string value, string option)
        {
            List<trangthai> list = new List<trangthai>();
            trangthai ls = null;
            ls = new trangthai();
            ls.name = option;
            ls.value = "0";
            list.Add(ls);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ls = new trangthai();
                    ls.name = dt.Rows[i][name].ToString();
                    ls.value = dt.Rows[i][value].ToString();
                    list.Add(ls);
                }
            }
            cb.DataSource = list;
            cb.DisplayMember = "name";
            cb.ValueMember = "value";
        }

        private void popLapdongho_Load(object sender, EventArgs e)
        {
            //loai dong ho
            clDH dh = new clDH();
            DataTable dtdh = dh.getdata();
            binCombo(cbldh, dtdh, "ten", "id", "<Chọn loại đồng hồ>");
            //to lap
            DataTable dttl = dh.gettolap();
            binCombo(cbtolap, dttl, "ten", "id", "<Chọn tổ lắp>");
        }

        public delegate void LapDH(int id);
        // khai báo 1 kiểu hàm delegate
        public LapDH MyGetData;

        private void btsave_Click(object sender, EventArgs e)
        {
            if (MyGetData != null)
            {
                Int64 idkh = idcus;
                string madh = txtmadh.Text.Trim();
                Int16 idloai = Int16.Parse((cbldh.SelectedValue == null) ? "0" : cbldh.SelectedValue.ToString().Trim());
                string kichthuoc = txtkichco.Text.Trim();
                Int64 ngaylap = Int64.Parse(dateday.Text.Trim().Split('/')[2].ToString() + dateday.Text.Trim().Split('/')[1].ToString() + dateday.Text.Trim().Split('/')[0].ToString());
                Int16 idtolap = Int16.Parse((cbtolap.SelectedValue == null) ? "0" : cbtolap.SelectedValue.ToString().Trim());
                string tolap = (cbtolap.SelectedValue == null || cbtolap.SelectedValue.ToString() == "0") ? "" : cbtolap.Text.Trim();

                //int chisoDHcu = int.Parse((txtCsDHcu.Text.Trim() == null) ? "0" : txtCsDHcu.Text.Trim());
                //int chisoDHmoi = int.Parse((txtCsDHmoi.Text.Trim() == null) ? "0" : txtCsDHmoi.Text.Trim());
                //string ctdutoan = txtchitiet.Text.Trim();
                //float tongdutoan = float.Parse((txtdutoan.Text.Trim()=="")?"0":txtdutoan.Text.Trim());



                Nullable<int> chisoDHcu;

                if (txtCsDHcu.Text == "")
                    chisoDHcu = null;
                else
                {
                    chisoDHcu = int.Parse(txtCsDHcu.Text.Trim());
                }

                Nullable<int> chisoDHmoi;

                if (txtCsDHmoi.Text == "")
                    chisoDHmoi = null;
                else
                {
                    chisoDHmoi = int.Parse(txtCsDHmoi.Text.Trim());
                }

                string ctdutoan = txtchitiet.Text.Trim();

               Nullable<float> tongdutoan;

                if (txtdutoan.Text == "")
                    tongdutoan = null;
                else
                {
                    tongdutoan = float.Parse(txtdutoan.Text.Trim());
                }


                string motalapdat = txtmotaLapDat.Text.Trim();
                //Int64 ngaydoc = Int64.Parse(dateday.Text.Trim().Split('/')[2].ToString() + dateday.Text.Trim().Split('/')[1].ToString() + dateday.Text.Trim().Split('/')[0].ToString());
                //string nvten = DataUser.UserName;
                clDH dh = new clDH();
                int outid = dh.thaydongdo(idkh, madh, idloai, kichthuoc, ngaylap, idtolap, tolap, chisoDHcu, chisoDHmoi, ctdutoan, tongdutoan, motalapdat);
                //update tbSudung_x
                Int16 index_tbSudung = -1;
                string str_tbSudung = "tbSudung" + ngaylap.ToString().Substring(0, 6);
                clTaoTbSuDung d = new clTaoTbSuDung();
                DataTable listsudung = d.listTableSudung();
                if (listsudung.Rows.Count > 0)
                {
                    SqlConnection sqlcon;
                    SqlCommand sqlcmd;
                    for (Int16 i = 0; i < listsudung.Rows.Count; i++)
                    {
                        if (listsudung.Rows[i][0].ToString().Trim() == str_tbSudung)
                        {
                            index_tbSudung = i;
                            break;
                        }
                    }
                    if (index_tbSudung != -1)
                    {
                        //for (Int16 i = index_tbSudung; i < listsudung.Rows.Count; i++)
                        //{

                            try
                            {
                                using (sqlcon = clHoadon.GetConnection())
                                {
                                    sqlcon.Open();
                                    sqlcmd = new SqlCommand("hh_update_tbsudung", sqlcon);
                                    sqlcmd.CommandType = CommandType.StoredProcedure;
                                    sqlcmd.CommandTimeout = 180;
                                    sqlcmd.Parameters.Add("@idkh_para", SqlDbType.BigInt).Value = idkh;
                                    sqlcmd.Parameters.Add("@tbsudung_name_para", SqlDbType.NVarChar, 30).Value = str_tbSudung; //listsudung.Rows[i][0].ToString();
                                    sqlcmd.ExecuteNonQuery();
                                    sqlcon.Close();
                                    sqlcon.Dispose();
                                }
                            }
                            catch { }
                        //}
                    }
                }
                //end updatte tbSudung_x
                MyGetData(outid);
            }
            this.Close();
        }

        private void btquit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}